import os
import cv2
import imutils
import argparse
import numpy as np 
import matplotlib.pyplot as plt
import tensorflow as tf 
import time
from imutils import paths
from keras.utils import np_utils
from keras.preprocessing.image import img_to_array
from sklearn.metrics import classification_report
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder


def data_loader_process(positives_data_path,negatives_data_path):

    data = []
    labels = []

    for imagePath in sorted(list(paths.list_images(positives_data_path))):

        image = cv2.imread(imagePath)
        image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        image = imutils.resize(image,width = 28)
        image = img_to_array(image)
        data.append(image)

        label = imagePath.split(os.path.sep)[-3]
        label = "smiling" if label == "positives" else "not_smiling"
        labels.append(label)


    for imagePath in sorted(list(paths.list_images(negatives_data_path))):
    
        image = cv2.imread(imagePath)
        image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
        image = imutils.resize(image,width = 28)
        image = img_to_array(image)
        data.append(image)

        label = imagePath.split(os.path.sep)[-3]
        label = "smiling" if label == "positives" else "not_smiling"
        labels.append(label)

    data = np.array(data,dtype = 'float') / 255.0
    labels = np.array(labels)
    le = LabelEncoder().fit(labels)
    labels = np_utils.to_categorical(le.transform(labels),2)


    classTotals = labels.sum(axis = 0)
    classWeight = classTotals.max() / classTotals


    (train_data,test_data,train_label,test_label) = train_test_split(data,labels,test_size = 0.20,stratify = labels,random_state = 42)

    return train_data,test_data,train_label,test_label





